package com.example.news;

import android.graphics.Color;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class NewsFragment extends Fragment {
    private String[] tabs = new String[] {"头条","社会","国内","国际","娱乐","体育","军事","科技","财经","时尚"};

    private ViewPager viewPagerTab;
    private TabLayout tabLayout;
    private View layoutView;
    public NewsFragment() {
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        layoutView = inflater.inflate(R.layout.fragment_news_layout,null,false);
        viewPagerTab = (ViewPager) layoutView.findViewById(R.id.viewPagerTab);
        tabLayout = (TabLayout) layoutView.findViewById(R.id.tabLayout);
        //初始化 Fragment
        List<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < tabs.length; i ++) {
            Fragment fragment = new NewsTabFragment();
            Bundle bundle = new Bundle();
            bundle.putString("type", SyncStateContract.Constants.types[i]);
            fragment.setArguments(bundle);
            fragments.add(fragment);
        }
        //为ViewPager设置Adapter
        viewPagerTab.setAdapter(new TabViewPagerAdapter(getChildFragmentManager(),tabs,getActivity(),fragments));
        //绑定tab和viewPager
        tabLayout.setupWithViewPager(viewPagerTab);
        //设置分别的颜色（选择和未选择）
        tabLayout.setTabTextColors(getResources().getColor(R.color.dark_white), Color.RED);
        return layoutView;
    }

    public interface NewsService {
        @GET("toutiao/index")
        Call<NewsResponse> getResponse(@Query("key") String key, @Query("type") String type);
    }

    /**
     * 访问网络更新界面
     */
    private void updateContent() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(SyncStateContract.Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        NewsService newsService = retrofit.create(NewsService.class);
        Call<NewsResponse> call = newsService.getResponse(Constants.API_KEY, mText);
        call.enqueue(new Callback<NewsResponse>() {
            @Override
            public void onResponse(Call<NewsResponse> call, Response<NewsResponse> response) {
                if (response.isSuccessful()) {
                    NewsResponse newsResponse = response.body();
                    NewsResponse.NewsResult result = newsResponse.getResult();
                    data = result.getData();
                    list.clear();//清空列表
                    for (int i = 0; i < data.length; i++) {
                        list.add(data[i]);
                    }
                    adapter.notifyDataSetChanged();
                    refreshView.setRefreshing(false);
                    mProgressBar.setVisibility(View.INVISIBLE);
                    Toast.makeText(getActivity(), "刷新成功", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<NewsResponse> call, Throwable t) {
                Toast.makeText(getActivity(), "获取数据失败，请检查网络设置", Toast.LENGTH_SHORT).show();
            }
        });
    }

}



